//
//  SFUserAccount.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/11.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit

class SFUserAccount: NSObject,NSCoding {

    /**
     "access_token" = "2.00BCAP3E7MF8dBe0122181a67nLRHD";
     "expires_in" = 157679999;  过期时间(秒)
     "remind_in" = 157679999;
     uid = 3993185765;      当前用户的ID
     */

    var access_token: String?

    var remind_in: Int = 0
    var uid: Int = 0

    var expires_in: Int = 0{

        didSet{

            //过期时间等于当前时间加过期时间
            expiresDate = NSDate().dateByAddingTimeInterval(Double(expires_in) )

        }
    }

    //过期时间
    var expiresDate: NSDate?

    //取到用户昵称
    var screen_name: String?
    //用户头像
    var avatar_large: String?


    init(dict: [String: AnyObject]) {
        super.init()

        setValuesForKeysWithDictionary(dict)
    }

    override func setValue(value: AnyObject?, forUndefinedKey key: String) {

    }


    override var description: String{

//        let keys = ["access_token","expires_in","remind_in","uid"]
//
//        return dictionaryWithValuesForKeys(keys).description

        return String("\(access_token) == \(expires_in) == \(remind_in) == \(uid) == \(screen_name) == \(avatar_large)")
    }

    // MARK: - coding的协议

    func encodeWithCoder(aCoder: NSCoder){

        aCoder.encodeObject(access_token, forKey: "access_token")
//        aCoder.encodeInteger(expires_in, forKey: "expires_in")
//        aCoder.encodeInteger(remind_in, forKey: "remind_in")
//        aCoder.encodeInteger(uid, forKey: "uid")

        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(remind_in, forKey: "remind_in")
        aCoder.encodeObject(uid, forKey: "uid")

        aCoder.encodeObject(expiresDate, forKey: "expiresDate")

        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")

    }

    required init?(coder aDecoder: NSCoder){

        access_token = aDecoder.decodeObjectForKey("access_token") as? String
//        expires_in = aDecoder.decodeIntegerForKey("expires_in") 
//        remind_in = aDecoder.decodeIntegerForKey("remind_in")
//        uid = aDecoder.decodeIntegerForKey("uid")
        expires_in = (aDecoder.decodeObjectForKey("expires_in") as? Int)!
        remind_in = (aDecoder.decodeObjectForKey("remind_in") as? Int)!
        uid = (aDecoder.decodeObjectForKey("uid") as? Int)!

        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String

        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate


    }


}
